// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd.service;

import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.collect.Maps;
import net.opentsdb.core.TSDB;
import net.opentsdb.tsd.HttpQuery;
import net.opentsdb.tsd.http.ServiceResponse;
import net.opentsdb.utils.status.ClearDataStatus;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 获取本地清除data的cache信息
 */
public class FetchClearDataCacheService extends RpcServiceImpl{

    private static final Logger LOG = LoggerFactory.getLogger(FetchClearDataCacheService.class);

    public FetchClearDataCacheService(TSDB tsdb, HttpQuery query) {
        super(tsdb, query);
    }

    @Override
    public void doGet() {
        doPost();
    }

    @Override
    public void doPost() {
        Map<String,String> map;
        Cache<String,String> cache = tsdb.getClearDataCache();

        String jobId = cache.getIfPresent(ClearDataStatus.JOB_ID);
        String paramJobId = ((Map<String,String>)params).get("jobId");

        if(Strings.isNullOrEmpty(jobId) || !jobId.equals(paramJobId)){
            map = Maps.newHashMap();
            map.put(ClearDataStatus.CLEAR_COUNT,"0");
            response = ServiceResponse.builder(true)
                    .response(map)
                    .build();
            return;
        }

        //cache还是存对象好一些，保持一致性。
        //这里适用改造enum然后用switch
        map = cache.asMap();
        if(ClearDataStatus.JobStatus.JOB_STATUS_FINISH.get()
                .equals(map.get(ClearDataStatus.JOB_STATUS))){
            response = ServiceResponse.builder(true)
                    .response(map)
                    .build();
        }else {
            response = ServiceResponse.builder(false)
                    .response(map)
                    .build();
        }
    }

    @Override
    public void checks() {
        Map<String,String> map = (Map<String,String>)params;

        if(!map.containsKey("jobId")){
            LOG.error("error get clear data cache request");

            response = ServiceResponse.builder(HttpResponseStatus.BAD_REQUEST)
                    .message("error delete type request, ex:{jobId:'10'}")
                    .build();
        }
    }
}
